#ifndef _MEMDC_H_
#define _MEMDC_H_

//////////////////////////////////////////////////
// CMemDC - memory DC
//
// Author: Keith Rule
// Email:  keithr@europa.com
// Copyright 1996-1997, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// This class implements a memory Device Context

//CXMemDC	modified by chishui
//Email		chishui2@gmail.com

class CXMemDC : public CDC 
{
private:
	HBITMAP		m_bitmap;
	HBITMAP		m_oldBitmap;
	HDC			m_oldDC;
	RECT		m_rcBounds;
public:
	CXMemDC(HDC hdc, const RECT& rcBounds) : CDC()
	{
		m_oldDC = hdc;
		CreateCompatibleDC(hdc);
		m_bitmap = CreateCompatibleBitmap(hdc, rcBounds.right - rcBounds.left, rcBounds.bottom - rcBounds.top);
		m_oldBitmap = (HBITMAP)SelectObject(m_hDC, m_bitmap);
		m_rcBounds = rcBounds;
	}
	~CXMemDC() 
	{
		::BitBlt(m_oldDC, m_rcBounds.left, m_rcBounds.top, m_rcBounds.right - m_rcBounds.left, m_rcBounds.bottom - m_rcBounds.top, 
					m_hDC, m_rcBounds.left, m_rcBounds.top, SRCCOPY);
		SelectObject(m_hDC, m_oldBitmap);
		DeleteObject(m_bitmap);
		DeleteDC();
	}
	operator HDC()
	{
		return m_hDC;
	}
};

#endif